const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter

const opt = {media_type:'',name:'', id:''}
const q = {id:'',q:'',answer:'',opt_list:[opt,opt,opt]}
// pages/sysConfig/config_gs/add_ti/add_ti.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    domainName,
    new_info:{},
    lis:[],
    windowWidth:wx.getSystemInfoSync().windowWidth,
  },
  back(){wx.navigateBack()},

  // 选择题目类型
  set_type(e){
    let index = e.detail.value
    let new_type = this.data.lis[index].id
    wx.request({
      url: domainName + '/gs_update_gsType',
      data: {stem_id:this.data.info.stem_id,type_id:new_type},...parameter,
      success: res => {
        if(res.data.update_res){
          wx.showToast({
            title: '更新完成！',
          })
          this.data.info.type_id = new_type
          this.setData({info:this.data.info})
        }
      },
    })
  },

  // 挑选图片
  chooseimg(){
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: res => {
        let tempFile = res.tempFiles[0]
        if(tempFile.size>100000){
          wx.showModal({
            content: '上传文件不宜大于100KB',
            showCancel: false,
            title: '提示',
          })
        }else{
          wx.uploadFile({
            filePath: tempFile.tempFilePath,
            name: 'files',
            url: domainName + '/gs_update_stemPic',
            formData: {
              stem_id:this.data.info.stem_id,
            },
            header : {'content-type':'application/x-www-form-urlencoded'},
            success: res => {
              if(JSON.parse(res.data).update_res){
                wx.showToast({
                  title: '更新完成！',
                })

                this.data.new_info.pic = tempFile.tempFilePath
                this.setData({new_info:this.data.new_info,info:this.data.new_info,tempFilePath:tempFile.tempFilePath})
              }
            },
          })
        }
      },
    })
  },

  // 输入题干
  input_title(e){
    this.data.new_info.title = e.detail.value
    this.setData({new_info:this.data.new_info})
  },
  update_title(){
    if(this.data.info.title==this.data.new_info.title){
      // 不需要提交
      return
    }
    wx.showModal({
      content: '确认修改吗？',
      showCancel: true,
      title: '提醒',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + '/gs_update_stemTitle',
            data: {
              stem_id:this.data.new_info.stem_id,
              title:this.data.new_info.title,
            },...parameter,
            success: res => {
              if(res.data.update_res){
                wx.showToast({
                  title: '更新完成！',
                })

                this.data.info.title = this.data.new_info.title
                this.setData({info:this.data.info})
              }
            },
          })
        }
      },
    })
  },


  // 输入解题思路
  input_analysis(e){
    this.data.new_info.analysis = e.detail.value
    this.setData({new_info:this.data.new_info})
  },
  update_analysis(e){
    if(this.data.info.analysis==this.data.new_info.analysis){
      // 不需要提交
      return
    }
    wx.showModal({
      content: '确认修改吗？',
      showCancel: true,
      title: '提醒',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + '/gs_update_stemAnalysis',
            data: {
              stem_id:this.data.new_info.stem_id,
              analysis:this.data.new_info.analysis,
            },...parameter,
            success: res => {
              if(res.data.update_res){
                wx.showToast({
                  title: '更新完成！',
                })

                this.data.info.analysis = this.data.new_info.analysis
                this.setData({info:this.data.info})
              }
            },
          })
        }
      },
    })
  },


  // 输入小题题目
  input_q(e){
    let index1 = e.currentTarget.dataset.index1
    let value = e.detail.value
    this.data.new_info.q_list[index1].q = value
    this.setData({new_info:this.data.new_info})
  },
  update_q(e){
    let index1 = e.currentTarget.dataset.index1
    if(this.data.new_info.q_list[index1].q==this.data.info.q_list[index1].q){
      // 不需要提交
      return
    }
    wx.showModal({
      content: '确认修改吗？',
      showCancel: true,
      title: '提醒',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + '/gs_update_stemQ',
            data: {
              stem_id:this.data.new_info.stem_id,
              q_id:this.data.new_info.q_list[index1].id || '',
              q:this.data.new_info.q_list[index1].q,
            },...parameter,
            success: res => {
              let q_id = res.data.q_id
              if(res.data.update_res || q_id){
                wx.showToast({title: '更新完成！'})

                if(q_id){
                  this.data.new_info.q_list[index1].id = q_id
                  this.data.info.q_list[index1].id = q_id
                  this.setData({new_info:this.data.new_info})
                }

                this.data.info.q_list[index1].q = this.data.new_info.q_list[index1].q
                this.setData({info:this.data.info})

              }
            },
          })
        }
      },
    })
  },



  // 输入选项
  input_opt(e){
    let index1 = e.currentTarget.dataset.index1
    let index2 = e.currentTarget.dataset.index2
    let value = e.detail.value
    this.data.new_info.q_list[index1].opt_list[index2].name = value
    this.data.new_info.q_list[index1].opt_list[index2].media_type = 1  //1-文字
    this.setData({new_info:this.data.new_info})
  },
  update_opt(e){
    let index1 = e.currentTarget.dataset.index1
    let index2 = e.currentTarget.dataset.index2

    if(this.data.info.q_list[index1].opt_list[index2].name==this.data.new_info.q_list[index1].opt_list[index2].name){
      // 不需要提交
      return
    }
    wx.showModal({
      content: '确认修改吗？',
      showCancel: true,
      title: '提醒',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + '/gs_update_stemOpt',
            data: {
              q_id:this.data.new_info.q_list[index1].id,
              opt_id:this.data.new_info.q_list[index1].opt_list[index2].id || '',
              opt:this.data.new_info.q_list[index1].opt_list[index2].name,
            },...parameter,
            success: res => {
              let opt_id = res.data.opt_id

              if(res.data.update_res || opt_id){
                wx.showToast({title: '更新完成！'})

                if(opt_id){
                  this.data.info.q_list[index1].opt_list[index2].id = opt_id
                  this.data.new_info.q_list[index1].opt_list[index2].id = opt_id
                  this.setData({new_info:this.data.new_info})
                }

                this.data.info.q_list[index1].opt_list[index2].name = this.data.new_info.q_list[index1].opt_list[index2].name
                this.setData({info:this.data.info})

              }
            },
          })
        }
      },
    })
  },
  // 删除选项
  del_opt(e){
    let index1 = e.currentTarget.dataset.index1
    let index2 = e.currentTarget.dataset.index2
    let opt_id = this.data.info.q_list[index1].opt_list[index2].id
    if(!opt_id){
      this.data.new_info.q_list[index1].opt_list.splice(index2,1)
      this.setData({new_info:this.data.new_info,info:this.data.new_info})
      return
    }
    wx.showModal({
      content: '确定要删除该选项吗？',
      title: '提醒',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + '/gs_delete_stemOpt',
            data: {opt_id},...parameter,
            success: res => {
              if(res.data.delete_res){
                this.data.new_info.q_list[index1].opt_list.splice(index2,1)
                this.setData({new_info:this.data.new_info,info:this.data.new_info})
                wx.showToast({title: '删除完成'})
              }
            },
          })
        }
      },
    })
  },

  // 增加选项
  add_opt(e){
    getApp().get_vibration()
    let index1 = e.currentTarget.dataset.index1
    this.data.info.q_list[index1].opt_list.push({media_type:'',name:'',id:''})
    this.data.new_info.q_list[index1].opt_list.push({media_type:'',name:'',id:''})
    this.setData({info:this.data.info, new_info:this.data.new_info})
  },

  // 增加问题
  add_q(){
    getApp().get_vibration()
    this.data.info.q_list.push({id:'',q:'',answer:'',opt_list:[{media_type:'',name:'',id:''},{media_type:'',name:'',id:''},{media_type:'',name:'',id:''}]})
    this.data.new_info.q_list.push({id:'',q:'',answer:'',opt_list:[{media_type:'',name:'',id:''},{media_type:'',name:'',id:''},{media_type:'',name:'',id:''}]})
    this.setData({info:this.data.info, new_info:this.data.new_info})
  },
  // 删除问题
  del_q(e){
    let index1 = e.currentTarget.dataset.index1
    let q_id = this.data.new_info.q_list[index1].id
    if(q_id){
      wx.showModal({
        content: '确定要删除该问题吗？',
        showCancel: true,
        title: '提醒',
        success: res => {
          if(res.confirm){
            wx.request({
              url: domainName + '/gs_delete_stemQ',
              data: {
                q_id:this.data.new_info.q_list[index1].id
              },...parameter,
              success: res => {
                if(res.data.delete_res){
                  this.data.new_info.q_list.splice(index1,1)
                  this.setData({new_info:this.data.new_info,info:this.data.new_info})
                  wx.showToast({title: '删除完成'})
                }
              },
            })
          }
        },
      })

    }
  },


  // 设置答案
  set_answer(e){
    let index1 = e.currentTarget.dataset.index1
    let value = e.detail.value
    let answer = this.data.new_info.q_list[index1].opt_list[value].id
    wx.request({
      url: domainName + '/gs_update_stemAnswer',
      data: {
        q_id:this.data.new_info.q_list[index1].id,
        answer:answer,
      },...parameter,
      success: res => {
        if(res.data.update_res){
          wx.showToast({
            title: '设置完成！',
          })

          this.data.new_info.q_list[index1].answer = answer
          this.setData({new_info:this.data.new_info})
        }
      },
    })
  },

  page_init(){
    wx.request({
      url: domainName + '/gs_add_ti_init',
      data: {},...parameter,
      success: res => {
        this.setData({
          lis:res.data.lis
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let info = JSON.parse(decodeURIComponent(options.info))
    this.setData({
      info:info,
      new_info:JSON.parse(JSON.stringify(info)),
    })
    this.page_init()
  },

})